package org.viettan.android.main.activities.articles;

import org.viettan.android.main.R;
import org.viettan.android.main.activities.common.BaseArticleActivity;
import org.viettan.android.main.intents.IntentKeys;
import org.viettan.android.main.intents.IntentUtility;
import org.viettan.android.main.io.DownloadEntityTask;
import org.viettan.android.main.io.EntityRetrievalException;
import org.viettan.android.main.managers.ArticleManager;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.model.Article;

import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.google.inject.Inject;

/**
 * Displays a single Viet Tan article inside a scrollview.
 */
public class WebArticleActivity extends BaseArticleActivity {
    @Inject
    private ArticleManager articleManager;
    @Inject
    private DialogManager notificationManager;
    @Inject
    private IntentUtility intentUtility;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	new DownloadArticleTask(this, notificationManager, true).execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.menu.open_share_menu, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.open_web:
	    intentUtility.sendBrowseIntent(this, article.getArticleWebUrl());
	    return true;
	case R.id.share:
	    intentUtility.sendShareIntent(this, article.getTitle(), article.getArticleWebUrl()
		    .toString());
	    return true;
	default:
	    return super.onOptionsItemSelected(item);
	}
    }

    private static class DownloadArticleTask extends DownloadEntityTask<WebArticleActivity, Article> {

	private final boolean useCache;

	private DownloadArticleTask(WebArticleActivity activity, DialogManager notificationManager,
		boolean useCache) {
	    super(activity, notificationManager);
	    this.useCache = useCache;
	}

	@Override
	protected Article getEntity() throws EntityRetrievalException {
	    WebArticleActivity activity = getActivity();
	    if (activity == null) {
		return null;
	    }
	    Uri articleUrl = (Uri) activity.getIntent().getExtras()
		    .get(IntentKeys.ARTICLE_XML_URL.name());
	    return activity.articleManager.getArticle(articleUrl, useCache);
	}

	@Override
	protected void displayEntity(Article article) {
	    WebArticleActivity activity = getActivity();
	    if (activity == null) {
		return;
	    }
	    activity.displayArticle(article);

	    // track the read of this article to Google Analytics
	    GoogleAnalyticsTracker.getInstance().trackEvent(
		    "Article" /* Category */,
		    "Read" /* Action */,
		    article.getTitle() /* Label */,
		    article.getId() /* Value */);
	}
    }
}
